﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc @Copy Right 2019
// 文件： CompanyRepository.cs
// 项目名称：斑马车务
// 创建时间：2019-07-31
// 负责人：lsc
// ===================================================================
using System;
using System.Text;
using System.Collections.Generic;
using YouWei.Infrastructure.Entity;
using YouWei.Infrastructure.Models;
using Dapper;
using Dapper.Extensions;
using YouWei.Utility;

namespace YouWei.Infrastructure.Repository
{
    /// <summary>
    ///企业表 仓储层
    /// </summary>	
    public class CompanyRepository : BaseRepository<Company>
    {

        #region 构造

        public CompanyRepository() : base() { }

        public CompanyRepository(string connectionString) : base(connectionString) { }

        public CompanyRepository(DatabaseContext dbContext) : base(dbContext) { }

        #endregion

        /// <summary>
        /// 查询公司列表
        /// 此方法仅用于管理员
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public Page<CompanyPageModel> Pages(int pageIndex, int pageSize, CompanySearchModel model)
        {
            StringBuilder sb = new StringBuilder("SELECT  T1.*,b.cName,b.pName,b.locationName FROM [Company] T1 WITH(NOLOCK) ");
            //注：base_locationAll表里的locationId是主键，userinfo表里的location是地区，意义不一样。所以这里拿Hcity=locationid   liucx
            sb.Append(" Left Join BaseLocation_All b WITH(NOLOCK) ON T1.City= b.locationId");
            sb.Append(" WHERE T1.IsActive=1");
            var dparams = new DynamicParameters();
            if (model != null)
            {
                AddParams(model, sb, dparams);
            }
            sb.Append(" ORDER BY T1.CompanyID DESC");
            return dbContext.Connection.Page<CompanyPageModel>(pageIndex, pageSize, sb.ToString(), dparams);
        }


        /// <summary>
        /// 构建查询条件
        /// </summary>
        /// <param name="model"></param>
        /// <param name="sb"></param>
        /// <param name="dparams"></param>
        private void AddParams(CompanySearchModel model, StringBuilder sb, DynamicParameters dparams)
        {
            if (!string.IsNullOrEmpty(model.ContactMobile))
            {
                sb.Append(" AND charindex(@ContactMobile,T1.ContactMobile)>0");
                dparams.Add("ContactMobile", model.ContactMobile, System.Data.DbType.String);
            }
            if (!string.IsNullOrEmpty(model.ContactUser))
            {
                sb.Append(" AND charindex(@ContactUser,T1.ContactUser)>0");
                dparams.Add("ContactUser", model.ContactUser, System.Data.DbType.String);
            }
            if (model.CreateUserID > 0)
            {
                sb.Append(" AND T1.CreateUserID=@CreateUserID");
                dparams.Add("CreateUserID", model.CreateUserID, System.Data.DbType.Int32);
            }
            if (model.HCity > 0)
            {
                sb.Append(" AND T1.City=@City");
                dparams.Add("City", model.HCity, System.Data.DbType.Int32);
            }
            else if (model.HProince > 0)
            {
                sb.Append(" AND T1.Proince=@Proince");
                dparams.Add("Proince", model.HProince, System.Data.DbType.Int32);
            }

            if (!string.IsNullOrEmpty(model.BCreateDate))
            {
                sb.Append(" AND T1.CreateDate>@BCreateDate");
                dparams.Add("BCreateDate", EConvert.ToDateTime(model.BCreateDate).Date, System.Data.DbType.DateTime);
            }

            if (!string.IsNullOrEmpty(model.ECreateDate))
            {
                sb.Append(" AND T1.CreateDate<@ECreateDate");
                dparams.Add("ECreateDate", EConvert.ToDateTime(model.ECreateDate).Date.AddDays(1), System.Data.DbType.DateTime);
            }
        }
    }
}